import produce from 'immer';
import httpUtil from '../../utils/HttpUtil';
import React from 'react';
let item2_table_1 = null;
function getTableData(url) {
    return new Promise((resolve, reject) => {
        httpUtil.get(url, null, (data) => {
            if (data.code === 200) {
                // item2_table_1 = data.data.item2_table_1;
                item2_table_1 = {
                    "area_name":[
                        "东北",
                        "辽宁",
                        "黑龙江",
                        "内蒙古",
                        "吉林",
                        "华东",
                        "安徽",
                        "江苏",
                        "上海",
                        "浙江",
                        "华中",
                        "湖南",
                        "湖北",
                        "江西",
                        "华北",
                        "北京",
                        "山东",
                        "河南",
                        "河北",
                        "山西",
                        "天津",
                        "华南",
                        "广东",
                        "广西",
                        "深圳",
                        "福建",
                        "海南",
                        "西北",
                        "陕西",
                        "甘肃",
                        "宁夏",
                        "青海",
                        "新疆",
                        "西南",
                        "四川",
                        "重庆",
                        "云南",
                        "贵州"
                    ],
                    "columns":[
                        {
                            "dateIndex":"big_area_name",
                            "title":" sdsd",
                            "key":"big_area_name"
                        },
                        {
                            "dateIndex":"area_name",
                            "title":" ",
                            "key":"area_name"
                        },
                        {
                            "dateIndex":"area_leader",
                            "title":"门诊量",
                            "key":"area_leader"
                        },
                        {
                            "dateIndex":"sales_map_02",
                            "title":"住院量",
                            "key":"sales_map_02"
                        },
                        {
                            "dateIndex":"sales_map_03",
                            "title":"门诊量增长率",
                            "key":"sales_map_03"
                        },
                        {
                            "dateIndex":"sales_map_04",
                            "title":"住院量增长率",
                            "key":"sales_map_03"
                        },
                    ],
                    "sales_map_05":[
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0
                    ],
                    "big_area_name":[
                        "东北",
                        "东北",
                        "东北",
                        "东北",
                        "东北",
                        "华东",
                        "华东",
                        "华东",
                        "华东",
                        "华东",
                        "华中",
                        "华中",
                        "华中",
                        "华中",
                        "华北",
                        "华北",
                        "华北",
                        "华北",
                        "华北",
                        "华北",
                        "华北",
                        "华南",
                        "华南",
                        "华南",
                        "华南",
                        "华南",
                        "华南",
                        "西北",
                        "西北",
                        "西北",
                        "西北",
                        "西北",
                        "西北",
                        "西南",
                        "西南",
                        "西南",
                        "西南",
                        "西南"
                    ],
                    "sales_map_04":[
                        391.57,
                        288.96,
                        0.00,
                        102.61,
                        0.00,
                        214.91,
                        91.27,
                        78.45,
                        33.94,
                        11.25,
                        118.71,
                        70.47,
                        48.24,
                        0.00,
                        978.90,
                        153.35,
                        561.15,
                        53.90,
                        94.70,
                        20.40,
                        95.40,
                        505.77,
                        251.14,
                        94.15,
                        47.68,
                        112.80,
                        0.00,
                        211.21,
                        42.05,
                        76.84,
                        3.42,
                        88.90,
                        0.00,
                        182.36,
                        68.54,
                        3.00,
                        97.82,
                        13.00
                    ],
                    "sales_map_03":[
                        1154.64,
                        574.14,
                        0.00,
                        580.50,
                        0.00,
                        742.43,
                        299.60,
                        224.85,
                        77.98,
                        140.00,
                        533.68,
                        157.68,
                        376.00,
                        0.00,
                        3293.48,
                        159.08,
                        1444.90,
                        349.50,
                        168.00,
                        368.20,
                        803.80,
                        318.40,
                        199.80,
                        40.40,
                        53.70,
                        24.50,
                        0.00,
                        267.30,
                        189.80,
                        77.50,
                        0.00,
                        0.00,
                        0.00,
                        2667.80,
                        1140.30,
                        114.00,
                        1121.70,
                        291.80
                    ],
                    "sales_map_02":[
                        16,
                        6,
                        4,
                        4,
                        2,
                        18,
                        6,
                        6,
                        4,
                        2,
                        14,
                        6,
                        5,
                        3,
                        34,
                        9,
                        7,
                        7,
                        5,
                        5,
                        1,
                        14,
                        6,
                        3,
                        3,
                        1,
                        1,
                        12,
                        6,
                        3,
                        2,
                        1,
                        0,
                        18,
                        10,
                        4,
                        3,
                        1
                    ],
                    "area_leader":[
                        "徐昕峣",
                        "相栋昌",
                        "孙宝臣",
                        "刘波",
                        "孙宝臣",
                        "刘金轶",
                        "孙鹏",
                        "王彦武",
                        "张建",
                        "杜祖庆",
                        "刘军",
                        "张晟",
                        "王倩",
                        "张晟",
                        "张德帅",
                        "何冬雪",
                        "邵先强",
                        "曲永辉",
                        "李鹏程",
                        "田培源",
                        "李鹏程",
                        "左彦卿",
                        "胡文琴",
                        "韩启",
                        "韩启",
                        "左彦卿",
                        "韩启",
                        "任铭",
                        "王雪",
                        "初昌辉",
                        "邰红光",
                        "任铭",
                        "任铭",
                        "张琴、赵亚冬",
                        "罗坚",
                        "黄玉华",
                        "覃庆康",
                        "毕胜杰"
                    ],
                };
                resolve(data.data);
            } else {
                reject(data.msg);
            }
        })
    })
}



let allColumns = [
    {
        title: '区域',
        dataIndex: 'area',
        key: 'area',
        align: 'center'
    },
    {
        title: '负责人',
        dataIndex: 'principal',
        key: 'principal',
        align: 'center'
    },
    {
        title: '人数(人)',
        dataIndex: 'average',
        key: 'average',
        align: 'center'
    },
    {
        title: '签约(万)',
        dataIndex: 'signing',
        key: 'signing',
        align: 'center'
    },
    {
        title: '回款(万)',
        dataIndex: 'returnedMoney',
        key: 'returnedMoney',
        align: 'center'
    },
    {
        title: '供应宝(个)',
        dataIndex: 'supply',
        key: 'supply',
        align: 'center'
    },
    {
        title: 'DRG(个)',
        dataIndex: 'DRGNum',
        key: 'DRGNum',
        align: 'center'
    },
    {
        title: 'HIA(个)',
        dataIndex: 'HIANum',
        key: 'HIANum',
        align: 'center'
    },
    {
        title: '机会池(万)',
        dataIndex: 'opportunity',
        key: 'opportunity',
        align: 'center'
    },
];
let dataMap = {};

allColumns.forEach((item) => {
    dataMap.key = item.key;
    dataMap[item.dataIndex] = (Math.round(Math.random() * 20901) + 19968).toString(16)
})
function getColumns(columns) {
    let newColumns = [];
    if (!columns) {
        newColumns = allColumns;
    } else {

    }
    return newColumns;
}

function getData() {
    let dataSource = [];
    for (let i = 0; i < 10; i++) {
        let newdata = produce(dataMap, (draft) => {
            draft.key = i;
        })
        dataSource.push(newdata);
    }
    return dataSource;
}
function renderContent(text, row, index) {
    const obj = {
        children: (<span>{text}</span>),
    };
    return obj
}
function getModel(data, str) {
    let fetchData = produce(data, (drag) => {
        let big_area_name = drag.big_area_name || [];
        drag.columns.forEach((e) => {
            e.dataIndex = e.dateIndex;
            e.render = renderContent;
        })
        for (var i = 0; i < big_area_name.length; i++) {
            if (big_area_name[i] != str) {
                for (var key in drag) {
                    if (key != 'columns') {
                        drag[key].splice(i, 1);
                    }
                }
                i--;
            }
        }
    });
    let dataSource = [];
    if (fetchData.big_area_name) {
        fetchData.big_area_name.forEach((el, i) => {
            const dataMap = {};
            fetchData.columns.forEach((item) => {
                dataMap[item.dataIndex] = fetchData[item.dateIndex][i];
                dataMap.key = i;
            })
            if (el != fetchData.area_name[i]) {
                dataSource.push(dataMap);
            }
        })
    }
    // const index = dataSource.findIndex((g) => { return g.area_name == '' });
    // const bigArea_nameMap = dataSource.splice(0, 1)[0];
    // if(bigArea_nameMap){
    //     bigArea_nameMap.area_name = `${str}大区`;
    //     dataSource.unshift(bigArea_nameMap);
    // }
    // dataSource.forEach((el) => {
    //     if (!el.area_leader) {
    //         el.area_leader = bigArea_nameMap.area_leader;
    //     }
    // })


    return {
        dataSource,
        columns: produce(fetchData.columns || [], (draft) => {
            draft.splice(0, 1)
        }) || [],
    }

}


function createTableModel(url, str, cb) {
    let model = {};
    if (url) {
        getTableData(url).then((data) => {
            let currentModel = getModel(data.item2_table_1, str);
            model.dataSource = currentModel.dataSource;
            model.columns = currentModel.columns;
            let big_area_name_list = Array.from(new Set(data.item2_table_1.big_area_name));
            let big_area_name_height = {};

            cb(model);
        }).catch((err) => {
            model.dataSource = getData();
            model.columns = getColumns();
            cb(model);
        })
    } else {
        if (item2_table_1) {
            let currentModel = getModel(item2_table_1, str);
            model.dataSource = currentModel.dataSource;
            model.columns = currentModel.columns;
            cb(model);
        }

    }

}
function getbigAreaHeight(item2_table_1) {
    if (!item2_table_1) {
        return
    }
    let big_area_name_list = Array.from(new Set(item2_table_1.big_area_name));
    let big_area_name_height = {};
    big_area_name_list.forEach(item => {
        let dataSource = getModel(item2_table_1, item).dataSource;
        big_area_name_height[item] = dataSource.length * parseInt(22 / 1024 * document.documentElement.clientHeight) + parseInt(35 / 1024 * document.documentElement.clientHeight);
    })
    return big_area_name_height;
}
export { createTableModel, getbigAreaHeight };